package com.vdg.fps;

import com.vdg.R;
import com.vdg.fps.services.FPSService;
import com.vdg.fps.ulti.Utilities;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;

public class SplashActivity extends Activity {
	private Handler mHandler;
	private static String TAG = "SplashActivity";
	private ServiceConnection mFpsServiceConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName name, IBinder service) {
			Log.v(TAG, "onServiceConnected start activity");
			onServiceReady();
		}

		public void onServiceDisconnected(ComponentName name) {
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.splash_layout);
		mHandler = new Handler();
		Intent fpsServiceIntent = new Intent(this, FPSService.class);
		if (Utilities.isMyServiceRunning(this)) {
			Log.v(TAG, "FPSService already run.");
		} else {
			Log.v(TAG, "FPSService is NOT run. Run first");
			startService(fpsServiceIntent);
		}
		bindService(fpsServiceIntent, mFpsServiceConnection, BIND_AUTO_CREATE);
	}

	private void onServiceReady() {
		mHandler.postDelayed(new Runnable() {
			@Override
			public void run() {
				Intent Main = new Intent(SplashActivity.this,
						MainActivity.class);
				startActivity(Main);
				SplashActivity.this.finish();
			}
		}, 1000);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		unbindService(mFpsServiceConnection);
		super.onDestroy();
	}
}
